﻿#!/usr/bin/python
# -*- utf-8 -*-

import smtplib, socket, sys, getopt, email, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.mime.text import MIMEText


# login
smtplogin="dgv"
# pass
smtpasswd="dgv06121974"
# addr smtp server
smtpserv="81.222.37.81"
# port smtp (25)
smtport=25
# sender addr
fromaddress = 'bugs@dgv.pp.ru'#''dgv@shm.ru

#
def send_mail(toaddress, actions, task, project, user):
    url = 'http://dgv.pp.ru/bugs/%s/task/%s/' % (project['name'], task['id'])
    task_info = 'Task #%s - %s' % (task['id'], ['summary']) 
    subject = 'Task #%s was changed' % task['id']
    action_string = ''
    for a in actions:
        action_string = action_string + '%s\n' % a     
    body = 'Bug control system report\n\nProject:  %s\nTask:     #%s\nSummary:  %s\n\n%s--------\nUser:     %s\nUrl:      %s'  % (project['name'], task['id'], task['summary'], action_string, user['username'], url)
    # create message body 
    mail = MIMEText(body, 'plain', 'UTF-8')
    mail['Subject'] = subject
    mail['To'] = ', '.join(toaddress)
    mail['From'] = fromaddress
    #print body
    #print mail.as_string()
    #sys.exit()
    try:
        server = smtplib.SMTP(smtpserv, smtport)
        server.set_debuglevel(0)
        server.ehlo()
        server.login(smtplogin, smtpasswd)
        server.sendmail(fromaddress, toaddress, mail.as_string())
        server.quit()
    except socket.sslerror:
        pass



def _main():
    task={}
    project={}
    user={}
    project['name'] = 'Test'
    project['norm_name'] = 'test'
    task['id'] = '123'
    task['summary'] = 'Тестирование посылки сообщений при изменения состояния задачи'
    user['username'] = 'Tester'
    toaddress = ['dgv@shm.ru']
    actions = ['- Status changed from New to InProgress','- Status changed from InProgress to Resolved','- Severity changed from Normal to Major']

    send_mail(toaddress, actions, task, project, user)    


	
if __name__ == '__main__':
    _main()





